﻿using System.Collections.Generic;
using System.Linq;
using Southern.Common.Cache;
using Southern.Models.Southern.Base;
using Southern.Models.Southern.Partials;

namespace Southern.Models.Southern
{
    public interface IUserRepository
    {
        List<SysUser> GetUsers();

        bool CreateUser(SysUser sysUser);

        bool UpdateUser(SysUser sysUser);

        SysUser GetUser(int userId);

        int DeleteUser(int userId);

        bool UserIsValid(string username, string password);
    }
    public class UserRepository : RepositoryBase, IUserRepository
    {
        public UserRepository(IDatabase db, ICache cacher)
            : base(db, cacher)
        {
        }

        public List<SysUser> GetUsers()
        {
            return SqlRetryPolicy.ExecuteAction<List<SysUser>>(() =>
                                                                   {
                                                                       return
                                                                           DB.GetManyRecords<SysUser>("spSys_UserSelectInfor").ToList();
                                                                   });
        }

        public bool CreateUser(SysUser sysUser)
        {
            return SqlRetryPolicy.ExecuteAction<bool>(() =>
                                                          {
                                                              return DB.InsertObject("spSys_UserInsert", sysUser);
                                                          });
        }

        public bool UpdateUser(SysUser sysUser)
        {
            return SqlRetryPolicy.ExecuteAction<bool>(() =>
                                                          {
                                                              return DB.UpdateObject("spSys_UserUpdate", sysUser);
                                                          });
        }

        public SysUser GetUser(int userId)
        {
            return SqlRetryPolicy.ExecuteAction<SysUser>(() =>
                                                             {
                                                                 return
                                                                     DB.GetSingleRecord<SysUser>("spSys_UserSelectInfoByUserId", userId);
                                                             });
        }

        public int DeleteUser(int userId)
        {
            return SqlRetryPolicy.ExecuteAction<int>(() =>
                                                          {
                                                              return DB.ExecuteNonQuery("spSys_UserDelete", userId);
                                                          });
        }

        public bool UserIsValid(string username, string password)
        {
            return SqlRetryPolicy.ExecuteAction<bool>(() =>
                                                          {
                                                              return DB.GetSingleRecord<bool>("spSys_UserIsValid", username, password);
                                                          });
        }
    }
}